<template>
  <div class="app-container">

    <el-card class="box-card">
      <el-form :rules="rules" ref="ruleForm" :model="ruleForm" label-width="150px" v-loading='loading'>

        <el-form-item label="特征码名称：" prop="username">
          <el-input v-model="ruleForm.username" />
        </el-form-item>
        <el-form-item label="特征码：" prop="detailsaddr">
          <el-input v-model="ruleForm.detailsaddr" />
        </el-form-item>

        <el-form-item>
          <el-button @click="handleCancel">取消</el-button>
          <el-button :loading='loading' type="primary" @click="handlePublish">保存</el-button>
        </el-form-item>

      </el-form>
    </el-card>

  </div>
</template>



<script>
import api from '@/api/featureCode'
export default {
  name: 'codeCreate',

  data() {
    return {
      loading: false,
      ruleForm: {
        username: '',
        detailsaddr: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入特征码名称', trigger: 'blur' }
        ],
        detailsaddr: [
          { required: true, message: '请输入特征码', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    headers() {
      return {
        'X-Hyxxkj-Admin-Token': getToken()
      }
    }
  },
  created() {},
  mounted() {},
  methods: {
    handleCancel: function() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.push({ path: './list' })
    },
    handlePublish() {
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          this.loading = true
          api
            .create(this.ruleForm)
            .then(res => {
              if (res.data.errno == 0) {
                this.$message({
                  message: '保存成功',
                  type: 'success'
                })
                this.$router.push({ path: './list' })
              } else {
                this.loading = false
                this.$message({
                  message: '保存失败...',
                  type: 'warning'
                })
              }
            })
            .catch(e => {
              this.loading = false
            })
        } else {
          this.$message({
            message: '请检查数据必填项',
            type: 'warning'
          })
          return false
        }
      })
    }
  }
}
</script>
